package com.rockcent.mall.lib.helper;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.utils.RBeanUtils;
import com.rockcent.mall.lib.domain.MallWechatMenuConfig;
import com.rockcent.mall.lib.dto.MallWechatMenuListDTO;
import com.rockcent.mall.lib.repository.MallWechatMenuConfigRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by Marco on 16-11-18.
 */
@Component
public class MallWechatMenuConfigHelper {

    private static Logger log = LoggerFactory.getLogger(MallWechatMenuConfigHelper.class);

    @Autowired
    private MallWechatMenuConfigRepository mallWechatMenuConfigRepository;

    /**
     * 验证菜单列表
     *
     * @param menuList
     * @param jsonResult
     * @return
     */
    public boolean checkMallWechatMenuList(List<MallWechatMenuListDTO> menuList, JsonResult jsonResult) {
        if (menuList == null || menuList.size() == 0) {
            jsonResult.remind("菜单列表 不能为空", log);
            return false;
        }
        for (MallWechatMenuListDTO mallWechatMenuListDTO : menuList) {
            if (!checkMallWechatMenu(mallWechatMenuListDTO, jsonResult)) {
                return false;
            }
            if (mallWechatMenuListDTO.getChildren() != null && mallWechatMenuListDTO.getChildren().size() > 0) {
                if (!checkMallWechatMenuList(mallWechatMenuListDTO.getChildren(), jsonResult)) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 验证菜单
     *
     * @param mallWechatMenuListDTO
     * @param jsonResult
     * @return
     */
    public boolean checkMallWechatMenu(MallWechatMenuListDTO mallWechatMenuListDTO, JsonResult jsonResult) {
        if (StringUtils.isBlank(mallWechatMenuListDTO.getName())) {
            jsonResult.remind("菜单名称 不能为空", log);
            return false;
        }
//        if(StringUtils.isBlank(mallWechatMenuListDTO.getMenuType())) {
//            jsonResult.remind("菜单类型 不能为空",log);
//            return false;
//        }
//        if(StringUtils.isBlank(mallWechatMenuListDTO.getPageUrl())) {
//            jsonResult.remind("菜单链接 不能为空",log);
//            return false;
//        }
        return true;
    }

    /**
     * 更新菜单列表,第一级菜单PARENT_ID为0
     *
     * @param menuList
     * @param mallId
     * @param parentId
     */
    public void saveList(List<MallWechatMenuListDTO> menuList, Long mallId, Long parentId) {
        for (MallWechatMenuListDTO menuDTO : menuList) {
            save(menuDTO, mallId, parentId);
        }
    }

    /**
     * 更新菜单
     *
     * @param mallWechatMenuListDTO
     * @param mallId
     * @param parentId
     */
    public void save(MallWechatMenuListDTO mallWechatMenuListDTO, Long mallId, Long parentId) {
        MallWechatMenuConfig mallWechatMenuConfig = new MallWechatMenuConfig();
        mallWechatMenuConfig.setMallId(mallId);
        mallWechatMenuConfig.setMenuType(mallWechatMenuListDTO.getMenuType());
        mallWechatMenuConfig.setName(mallWechatMenuListDTO.getName());
        mallWechatMenuConfig.setPageUrl(mallWechatMenuListDTO.getPageUrl());
        mallWechatMenuConfig.setMaterialType(mallWechatMenuListDTO.getMaterialType());
        mallWechatMenuConfig.setMediaId(mallWechatMenuListDTO.getMediaId());
        mallWechatMenuConfig.setMaterialName(mallWechatMenuListDTO.getMaterialName());
        mallWechatMenuConfig.setMaterialUrl(mallWechatMenuListDTO.getMaterialUrl());
        if (parentId != null) {
            mallWechatMenuConfig.setParentId(parentId);
        }
        mallWechatMenuConfig = mallWechatMenuConfigRepository.save(mallWechatMenuConfig);
        if (mallWechatMenuListDTO.getChildren() != null && mallWechatMenuListDTO.getChildren().size() > 0) {
            saveList(mallWechatMenuListDTO.getChildren(), mallId, mallWechatMenuConfig.getId());
        }
    }

    /**
     * 通过ID获取树形菜单
     *
     * @param id
     * @param mallId
     * @return
     */
    public MallWechatMenuListDTO getMenuTree(Set<Long> ids, Long id, Long mallId) {
        MallWechatMenuListDTO mallWechatMenuListDTO = getMenuById(id);
        if (mallWechatMenuListDTO != null) {
            ids.add(mallWechatMenuListDTO.getId());
            List<MallWechatMenuListDTO> subMenuList = getSubMenuByParentId(mallId, mallWechatMenuListDTO.getId());
            if (subMenuList != null && subMenuList.size() > 0) {
                for (MallWechatMenuListDTO dto : subMenuList) {
                    MallWechatMenuListDTO menuListDTO = getMenuTree(ids, dto.getId(), mallId);
                    ids.add(menuListDTO.getId());
                    mallWechatMenuListDTO.getChildren().add(menuListDTO);
                }
            }
        }
        return mallWechatMenuListDTO;
    }

    /**
     * 通过ID获取菜单
     *
     * @param id
     * @return
     */
    public MallWechatMenuListDTO getMenuById(Long id) {
        MallWechatMenuListDTO mallWechatMenuListDTO = null;
        MallWechatMenuConfig mallWechatMenuConfig = mallWechatMenuConfigRepository.findOne(id);
        if (mallWechatMenuConfig != null) {
            mallWechatMenuListDTO = new MallWechatMenuListDTO();
            RBeanUtils.copyProperties(mallWechatMenuConfig, mallWechatMenuListDTO);
        }
        return mallWechatMenuListDTO;
    }

    /**
     * 通过PARENTID获取子菜单列表
     *
     * @param mallId
     * @param parentId
     * @return
     */
    public List<MallWechatMenuListDTO> getSubMenuByParentId(Long mallId, Long parentId) {
        List<MallWechatMenuConfig> subMenuList = mallWechatMenuConfigRepository.findByMallIdAndParentId(mallId, parentId);
        return convertPO2DTO(subMenuList);
    }

    /**
     * PO trans DTO
     *
     * @param menuList
     * @return
     */
    public List<MallWechatMenuListDTO> convertPO2DTO(List<MallWechatMenuConfig> menuList) {
        List<MallWechatMenuListDTO> menuDTOList = null;
        if (menuList != null && menuList.size() > 0) {
            menuDTOList = new ArrayList<>();
            MallWechatMenuListDTO mallWechatMenuListDTO = null;
            for (MallWechatMenuConfig menu : menuList) {
                mallWechatMenuListDTO = new MallWechatMenuListDTO();
                RBeanUtils.copyProperties(menu, mallWechatMenuListDTO);
                menuDTOList.add(mallWechatMenuListDTO);
            }
        }
        return menuDTOList;
    }

    /**
     * 通过列表生成树形菜单
     *
     * @param list
     * @param parentId
     * @return
     */
    public List<MallWechatMenuListDTO> getChildTreeObjects(List<MallWechatMenuListDTO> list, Long parentId) {
        List<MallWechatMenuListDTO> returnList = new ArrayList<MallWechatMenuListDTO>();
        if (list != null && list.size() > 0) {
            for (Iterator<MallWechatMenuListDTO> iterator = list.iterator(); iterator.hasNext(); ) {
                MallWechatMenuListDTO res = (MallWechatMenuListDTO) iterator.next();
                /**
                 * 判断第一个对象是否为第一个节点
                 *
                 */
                if (res.getParentId() != null && parentId != null && res.getParentId().longValue() == parentId.longValue()) {
                    /**
                     * 相等--说明第一个节点为父节点--递归下面的子节点
                     */
                    recursionFn(list, res);
                    returnList.add(res);
                }
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    public void recursionFn(List<MallWechatMenuListDTO> list, MallWechatMenuListDTO t) {
        List<MallWechatMenuListDTO> childsList = getChildList(list, t);
        /**
         * 设置他的子集对象集
         */
        t.setChildren(childsList);
        /**
         * 迭代--这些子集的对象--时候还有下一级的子级对象
         */
        for (MallWechatMenuListDTO nextChild : childsList) {
            /**
             * 下一个对象，与所有的资源集进行判断
             */
            if (hasChild(list, nextChild)) {
                /**
                 * 有下一个子节点,递归
                 */
                Iterator<MallWechatMenuListDTO> it = childsList.iterator();
                while (it.hasNext()) {
                    MallWechatMenuListDTO node = it.next();
                    /**
                     * 所有的对象--跟当前这个childsList 的对象子节点
                     */
                    recursionFn(list, node);
                }
            }
        }
    }

    /**
     * 获得指定节点下的所有子节点
     */
    public List<MallWechatMenuListDTO> getChildList(List<MallWechatMenuListDTO> list, MallWechatMenuListDTO t) {
        List<MallWechatMenuListDTO> childsList = new ArrayList<MallWechatMenuListDTO>();
        Iterator<MallWechatMenuListDTO> it = list.iterator();
        while (it.hasNext()) {
            MallWechatMenuListDTO child = it.next();
            /**
             * 判断集合的父ID是否等于上一级的id
             */
            if (child.getParentId() != null && t.getId() != null && child.getParentId().longValue() == t.getId().longValue()) {
                childsList.add(child);
            }
        }
        return childsList;
    }

    /**
     * 判断是否还有下一个子节点
     */
    public boolean hasChild(List<MallWechatMenuListDTO> list, MallWechatMenuListDTO t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
